package com.yueya.basic.msg;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;
import com.wj.jpush.model.Push;
import com.yueya.common.util.DateUtil;
import com.yueya.common.util.UUIDGenerator;

public class Msg extends Model<Msg> {

	private static final long serialVersionUID = -6985754239540245975L;

	public static Msg dao = new Msg();

	// 0：系统消息'
	/**
	 * 系统消息
	 * 
	 * 0-0 0-1 0-2 0-3
	 * 
	 * 
	 */
	public static final String SYSMSG = "0";

	public Record getLastMsg(String type, String accountId) {
		String sql = "select * from t_msg m inner join t_msg_account ma  on m.id =ma.msg_id  where m.type =? and ma.del_flag ='0' and ma.user_id=? order by m.create_time desc limit 1  ";
		return Db.findFirst(sql, type, accountId);
	}

	/**
	 * 设置消息已读
	 * 
	 * @param msgId
	 * @param accountId
	 * @return
	 */
	public int setReaded(String msgId, String accountId) {
		return Db.update("update t_msg_account set is_read = '1' where  msg_id = ? and user_id=?", msgId, accountId);
	}

	/**
	 * 移除消息对象
	 * 
	 * @param msgId
	 * @param accountId
	 * @return
	 */
	public int removeMsg(String msgId, String accountId) {
		return Db.update("update t_msg_account set del_flag = '1' where  msg_id = ? and user_id=?", msgId, accountId);
	}

	/**
	 * 获取未读消息
	 * 
	 * @param accountId
	 * @return
	 */
	public long getMsgNum(String accountId) {
		return Db.findFirst(
				"select count(*) num from  t_msg_account where del_flag ='0' and  is_read ='0' and user_id=?",
				accountId).getLong("num");
	}

	/**
	 *  需改密码发送的消息
	 * 
	 * @param accountId
	 * @return
	 */
	public boolean addUpdatePwd(String accountId) {
		// 注册成功设置
		boolean bl = false;
		String txt = "您在吉集号平台修改密码,已经成功！";
		Record msg = new Record();
		msg.set("id", UUIDGenerator.getUUID());
		msg.set("user_id", "10000");// 10000 万吉平台账号
		msg.set("title", "吉集号平台提醒");
		msg.set("content_text", txt);
		msg.set("content_html", "<p>" + txt + "</p>");
		msg.set("type", SYSMSG);// 消息类型，0：系统消息，1：货源消息，2：询价通知
		msg.set("create_time", DateUtil.getDate(DateUtil.timeFormat));
		// 系统通知
		if (Db.save("t_msg", msg)) {
			bl = addMsgAccount(msg.getStr("id"), accountId);
		}
		return bl;
	}

	/**
	 * 注册成功！
	 * 
	 * @return
	 */
	public boolean addMsgRegSuccess(String accountid) {
		boolean bl = false;
		String txt = "恭喜您在吉集号注册成功！";
		Record msg = new Record();
		msg.set("id", UUIDGenerator.getUUID());
		msg.set("user_id", "10000");// 10000 万吉平台账号
		msg.set("title", "欢迎加入吉集号");
		msg.set("content_text", txt);
		msg.set("content_html", "<p>" + txt + "</p>");
		msg.set("type", SYSMSG);// 消息类型，0：系统消息，1：货源消息，2：询价通知
		// 3:报价通知
		msg.set("create_time", DateUtil.getDate(DateUtil.timeFormat));
		Db.save("t_msg", msg);
		// 系统通知
		bl = addMsgAccount(msg.getStr("id"), accountid);

		return bl;
	}

	/**
	 * 
	 * @param msgid
	 * @param accountid
	 * @return
	 */
	public boolean addMsgAccount(String msgid, String accountid) {
		Record msgAccount = new Record();
		msgAccount.set("id", UUIDGenerator.getUUID());
		msgAccount.set("msg_id", msgid);
		msgAccount.set("user_id", accountid);// 消息发送的对象
		return Db.save("t_msg_account", msgAccount);
	}

	public boolean push(String title, String msg, String sentEvent, String... user) {
		boolean bl = true;

		// for (String ur : user) {
		// System.out.println("ur-------"+ur);
		// }

		try {
			Push.pushByUser(title, msg, sentEvent, user);
			// System.out.println("极光推送成功！");
		} catch (Exception e) {
			// System.out.println("极光推送接口请求异常！");
			bl = false;
			// System.out.println(e);
			// TODO: handle exception
		}

		return bl;
	}

	public static void main(String[] args) {
		Msg msg = new Msg();
		msg.push("xxx", "yyy", SYSMSG, "4833065b4077a9f8a498fa811cee");
	}

}